﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using Korbit.Data;
using Korbit.Common;
using Korbit;
using Korbit.Constant;

using BTCe;
using BTCe.Data;
using BTCe.Common;
using BTCe.Constant;

using ETC;

using BitcoinArbitrage.Data;


namespace BitcoinArbitrage
{
    public partial class frmMainframe : Form
    {
        ArbitrageData _controller;

        public frmMainframe()
        {
            InitializeComponent();

            initialzied();
        }

        private void initialzied()
        {
            _controller = new ArbitrageData();
        }

        private void btnKorbit_Click(object sender, EventArgs e)
        {
            KorbitTestFrame korbitTest = new KorbitTestFrame();

            korbitTest.Visible = true;
        }
        
        private void dgvKorbitAsk_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnBTCe_Click(object sender, EventArgs e)
        {
            BTCeTestFrame btceTest = new BTCeTestFrame();

            btceTest.Visible = true;
        }

        private void btnETC_Click(object sender, EventArgs e)
        {
            Console.WriteLine(ETC.CurrencyRate.CurrentRate());
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void frmMainframe_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void Login()
        {
            //컨트롤 개체가 생성이 안 되어 있는 경우
            if (_controller == null)
            {
                _controller = new ArbitrageData();
            }

            //로그인을 한다.
            _controller.Login();

            //로그인 여부를 확인한다.
            if (_controller.IsLogin())
            {
                _controller.RefreshData();

                lblKorbitLogStatus.Text = "로그인";
                lblRefreshTime.Text = _controller.RefreshTokenTime().ToString("T");
            }
            else
            {
                lblKorbitLogStatus.Text = "로그아웃";
                lblRefreshTime.Text = "00:00:00";
            }
        }

        private void GetInfo()
        {
            //로그인이 안 되어 있을 경우 패스한다.
            if (!_controller.IsLogin())
                return;

            //환율 정보를 받아온다.
            lblCurrency.Text = _controller.Currency().ToString("N02");

            //자산 정보를 받아온다.
            //원화자산
            lblKRWCurrency.Text = _controller.BalanceKRWCurrency().ToString("N02");
            lblKRWBTCAmount.Text = _controller.BalanceKRWBTC().ToString("N02");
            lblUSDCurrency.Text = _controller.BalanceUSDCurrency().ToString("N02");
            lblUSDBTCAmount.Text = _controller.BalanceUSDBTC().ToString("N02");

            //전체 자산
            lblKRWCurrencyAsset.Text = lblKRWCurrency.Text;
            lblUSDCurrencyAsset.Text = lblUSDCurrency.Text;
            lblBTCAsset.Text = (_controller.BalanceKRWBTC() + _controller.BalanceUSDBTC()).ToString("N02");

            txtActiveMarginalBuy.Text = _controller.MaginalBuyProfit().ToString("N0");
            txtActiveMarginalSell.Text = _controller.MarginalSellProfit().ToString("N0");

        }

        private void BindPublicExchangeData()
        {
            //데이터 바인딩
            dgvKorbitAsk.DataSource = _controller.PublicExchangeKorbitAsk();
            dgvKorbitBid.DataSource = _controller.PublicExchangeKorbitBid();
            dgvBTCeAsk.DataSource = _controller.PublicExchangeBTCeAsk();
            dgvBTCeBid.DataSource = _controller.PublicExchangeBTCeBid();

        }

        private void lblInfo_Click(object sender, EventArgs e)
        {
            //로그인이 안 되어 있을 경우 패스한다.
            if (!_controller.IsLogin())
            {
                MessageBox.Show("로그인을 먼저 해주세요.");
                return;
            }                

            GetInfo();

            BindPublicExchangeData();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (btnRun.Text == "액티브 매매 시작")
            {
                ActivateTrading();
                btnRun.Text = "액티브 매매 중단";
            }
            else
            {
                DeactivateTrading();

                btnRun.Text = "액티브 매매 시작";
            }
            

        }

        private void ActivateTrading()
        {
            if (tmrBase.Enabled == false)
            {
                this.tmrBase.Interval = 15000;
                this.tmrBase.Enabled = true;
            }
        }

        private void DeactivateTrading()
        {
            this.tmrBase.Enabled = false;
        }

        private void ActivateTimer(object sender, EventArgs e)
        {
            RunTrading();
        }

        private void RunTrading()
        {
            _controller.RefreshData();

            _controller.RefreshToken();

            lblRefreshTime.Text = _controller.RefreshTokenTime().ToString("T");

            GetInfo();

            BindPublicExchangeData();

            _controller.TradingMarginal();

        }

    }
}
